/*
 * ModifyItem.java
 */

package nxtgeninventorysystem.View.Dialog;

import java.awt.event.ActionEvent;
import java.util.HashMap;
import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import nxtgeninventorysystem.Controller.Actions;
import nxtgeninventorysystem.Data.Item;
import nxtgeninventorysystem.View.Gui;
import nxtgeninventorysystem.View.Gui.ComboBoxType;
import org.jdesktop.application.Action;

/**
 * The dialog box that displays fields for item creation.
 * @author Seraph Softworks
 */
public class ModifyItem extends GuiDialog {

    /** Creates new form DatabaseConfig */
    public ModifyItem(java.awt.Frame parent) {
        super(parent);
    }

    @Override
    public void resetFields() {
        if(target == null) {
            itemDescriptionValue.setText("");
            itemNameValue.setText("");
            itemTypeValue.setSelectedIndex(0);
        }
        else {
            itemDescriptionValue.setText(target.getDescription());
            itemNameValue.setText(target.getName());
            itemTypeValue.setSelectedItem(target.getType());
        }
    }

    @Override
    public void refreshComboBoxes(HashMap<ComboBoxType, DefaultComboBoxModel> models) {
        itemTypeValue.setModel(models.get(Gui.ComboBoxType.ItemTypesComboBox));
    }

    @Override
    protected void initialize() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        closeButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();
        javax.swing.JLabel itemNameLabel = new javax.swing.JLabel();
        javax.swing.JLabel itemDescriptionLabel = new javax.swing.JLabel();
        itemNameValue = new javax.swing.JTextField();
        javax.swing.JScrollPane itemDescScrollPane = new javax.swing.JScrollPane();
        itemDescriptionValue = new javax.swing.JTextArea();
        javax.swing.JLabel itemTypeLabel = new javax.swing.JLabel();
        itemTypeValue = new javax.swing.JComboBox();
        javax.swing.JButton createItemType = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(nxtgeninventorysystem.Driver.class).getContext().getResourceMap(ModifyItem.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(375, 196));
        setName("Form"); // NOI18N
        setResizable(false);

        closeButton.setAction(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                dispose();
                Gui.setStatusBarMessage("Ready...");
            }
        });
        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(nxtgeninventorysystem.Driver.class).getContext().getActionMap(ModifyItem.class, this);
        okButton.setAction(actionMap.get("ok")); // NOI18N
        okButton.setText(resourceMap.getString("okButton.text")); // NOI18N
        okButton.setName("okButton"); // NOI18N

        itemNameLabel.setText(resourceMap.getString("itemNameLabel.text")); // NOI18N
        itemNameLabel.setName("itemNameLabel"); // NOI18N

        itemDescriptionLabel.setText(resourceMap.getString("itemDescriptionLabel.text")); // NOI18N
        itemDescriptionLabel.setName("itemDescriptionLabel"); // NOI18N

        itemNameValue.setName("itemNameValue"); // NOI18N

        itemDescScrollPane.setName("itemDescScrollPane"); // NOI18N

        itemDescriptionValue.setColumns(20);
        itemDescriptionValue.setRows(5);
        itemDescriptionValue.setName("itemDescriptionValue"); // NOI18N
        itemDescScrollPane.setViewportView(itemDescriptionValue);

        itemTypeLabel.setText(resourceMap.getString("itemTypeLabel.text")); // NOI18N
        itemTypeLabel.setName("itemTypeLabel"); // NOI18N

        itemTypeValue.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Choose Type--" }));
        itemTypeValue.setName("itemTypeValue"); // NOI18N

        createItemType.setAction(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                Actions.item.gotoCreateItemType();
            }
        });
        createItemType.setText(resourceMap.getString("createItemType.text")); // NOI18N
        createItemType.setName("createItemType"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(itemNameLabel)
                            .addComponent(itemTypeLabel)
                            .addComponent(itemDescriptionLabel))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(itemTypeValue, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(createItemType))
                            .addComponent(itemDescScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
                            .addComponent(itemNameValue, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(okButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(closeButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(itemNameValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(itemNameLabel))
                .addGap(3, 3, 3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(itemTypeValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(itemTypeLabel)
                    .addComponent(createItemType))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(itemDescriptionLabel)
                    .addComponent(itemDescScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(okButton)
                    .addComponent(closeButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JTextArea itemDescriptionValue;
    private javax.swing.JTextField itemNameValue;
    private javax.swing.JComboBox itemTypeValue;
    private javax.swing.JButton okButton;
    // End of variables declaration//GEN-END:variables

    private Item target = null;

    public void setTargetItem(Item item) {
        target = item;
    }

    public void dropTargetItem() {
        target = null;
    }

    public Item getFormItem() {
        Item form = new Item();
        form.setName(itemNameValue.getText());
        form.setDescription(itemDescriptionValue.getText());
        form.setType((String) itemTypeValue.getSelectedItem());
        return form;
    }

    public Item getTargetItem() {
        return target;
    }

    @Override
    public void setOkAction(javax.swing.Action action) {
        okButton.setAction(action);
        okButton.setText("Ok");
    }

}
